var $userCode, $passCode, $returnUrl, $opts;
$(function () {
	$userCode = $('#idUserCode');
	$passCode = $('#idPassCode');

	var opts = getSearchWords(window.location.search);
	if (opts.ReturnUrl) {
		var query = createSearchWords(opts, true);
		if (query != "")
			query = "?" + query;
		$returnUrl = opts.ReturnUrl + query;
	}
	else
		$returnUrl = '/';

	$('#idSubmit').click(function () {
		signIn();
	});
})

document.onkeydown = function (e) {
	var ev = document.all ? window.event : e;
	if (ev.keyCode == 13) {
		signIn();
	}
}

function signIn() {
	var userCode = $userCode.val();
	var passCode = $passCode.val();
	if (userCode.length == 0 || passCode.length == 0) {
		alert('Please Enter UserName Or PassWord！');
		return;
	}
	$.post('/api/login', { usercode: userCode, passcode: passCode }, function (data) {
		if (data.code == 0) {
			location = $returnUrl;
		} else
			alert(data.msg);
	}, 'json');
}